package hotel.switchs.oversea.biz.service.impl;

import cn.hutool.core.util.ObjectUtil;
import hotel.base.oversea.constant.CommonConstants;
import hotel.base.oversea.service.impl.BaseServiceImpl;
import hotel.base.oversea.utils.Result;
import hotel.switchs.oversea.api.VO.request.GetRoomListVo;
import hotel.switchs.oversea.api.entity.SwitchCancelRuleSetEntity;
import hotel.switchs.oversea.biz.mapper.SwitchCancelRuleSetMapper;
import hotel.switchs.oversea.biz.service.SwitchCancelRuleSetService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Auther: 你的名字
 * @Date: 2020/09/24/13:50
 * @Description:
 */
@Service
@Slf4j
public class SwitchCancelRuleSetServiceImpl  extends BaseServiceImpl<SwitchCancelRuleSetEntity> implements SwitchCancelRuleSetService {
    @Autowired
    private SwitchCancelRuleSetMapper switchCancelRuleSetMapper;
    /***
    * @Author: lmf
    * @Date: 2020/9/24
     * 上传取消规则的酒店
    */
    @Override
    public Result<Object> upCancel(MultipartFile excelFile) {
        List<SwitchCancelRuleSetEntity> switchCancelRuleSetEntities=new ArrayList<>();
        try {
            Workbook workbook= WorkbookFactory.create(excelFile.getInputStream());
            excelFile.getInputStream().close();
            //工作表对象
            Sheet sheet=workbook.getSheetAt(0);
            tab:for(int i=1;i<sheet.getPhysicalNumberOfRows();i++){
                Row sheetRow=sheet.getRow(i);
                Cell cell=sheetRow.getCell(0);
                Double hotelId=cell.getNumericCellValue();
                cell=sheetRow.getCell(1);
                Double roomId=cell.getNumericCellValue();
                SwitchCancelRuleSetEntity switchCancelRuleSetEntity=switchCancelRuleSetMapper.selectByHotelIdAndRoomId(hotelId.intValue(),roomId.intValue());
                if(!ObjectUtil.isEmpty(switchCancelRuleSetEntity)){
                    continue tab;
                }else{
                    switchCancelRuleSetEntity=switchCancelRuleSetMapper.selectByHotelId(hotelId.intValue());
                    if(!ObjectUtil.isEmpty(switchCancelRuleSetEntity)){
                        continue tab;
                    }
                }
                switchCancelRuleSetEntity=new SwitchCancelRuleSetEntity();
                switchCancelRuleSetEntity.setCreateTime(new Date());
                switchCancelRuleSetEntity.setModifyTime(new Date());
                switchCancelRuleSetEntity.setHotelId(hotelId.intValue());
                switchCancelRuleSetEntity.setRoomId(roomId.intValue());
                switchCancelRuleSetEntity.setRemark("");
                switchCancelRuleSetEntity.setTime("21");
                switchCancelRuleSetEntities.add(switchCancelRuleSetEntity);
            }
        } catch (InvalidFormatException | IOException e) {
            e.printStackTrace();
        }
        if(switchCancelRuleSetEntities.size()>0){
            int result=switchCancelRuleSetMapper.insertList(switchCancelRuleSetEntities);
            if(result>0){
                return new Result<>();
            }else{
                return new Result<>("导入失败");
            }
        }else{
            return new Result<>("导入失败");
        }
    }
    /***
    * @Author: lmf
    * @Date: 2020/9/24
     * 修改最晚取消时间点
    */
    @Override
    public Result<Object> setCancmodifyCancelRuleelRule(GetRoomListVo getRoomListVo) {
        int result=switchCancelRuleSetMapper.updateTime(getRoomListVo.getTime());
        if(result>0){
            return new Result<>(CommonConstants.SUCCESS,"修改取消规则成功");
        }else{
            return new Result<>("修改取消规则失败");
        }
    }
}
